package com.example.demo.service.Impl;

import com.example.demo.entity.ParkingLot;
import com.example.demo.entity.ParkingLotRecord;
import com.example.demo.mapper.ParkingLotMapper;
import com.example.demo.mapper.ParkingLotRecordMapper;
import com.example.demo.pojo.ParkingLotDTO;
import com.example.demo.pojo.ParkingLotVO;
import com.example.demo.service.ParkingLotService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ParkingLotServiceImpl implements ParkingLotService {
    @Resource
    ParkingLotMapper parkingLotMapper;
    @Resource
    ParkingLotRecordMapper parkingLotRecordMapper;

    @Override
    public List<ParkingLot> selectByDTO(ParkingLotDTO parkingLotDTO){
        return parkingLotMapper.selectByDTO(parkingLotDTO);
    }

    @Override
    public Integer selectEntryCarNum(Integer parkingLotId){
        List<ParkingLotRecord> parkingLotRecords = parkingLotRecordMapper.selectByParkingLotId(parkingLotId);
        Integer entry = 0;
        Integer departure = 0;
        for (ParkingLotRecord parkingLotRecord: parkingLotRecords) {
            if(parkingLotRecord.getEntry() == 1){
                entry ++;
            }
            else{
                departure ++;
            }
        }
        return entry - departure;
    }

    @Override
    public List<ParkingLotVO> selectVO(ParkingLotDTO parkingLotDTO) {
        List<ParkingLot> parkingLots = parkingLotMapper.selectByDTO(parkingLotDTO);
        List<ParkingLotVO> parkingLotVOS = new ArrayList<>();
        for(ParkingLot parkingLot: parkingLots){
            Integer availableSpots = parkingLot.getCapacity() - this.selectEntryCarNum(parkingLot.getId());
            ParkingLotVO parkingLotVO = new ParkingLotVO(parkingLot.getName(),availableSpots,parkingLot.getCapacity());
            parkingLotVOS.add(parkingLotVO);
        }
        return parkingLotVOS;
    }
}
